<?php
/**
 * WHQM router implementation
 *
 * This file contains class WHQM_Router which in charge to call proper controller based on the URL

 * @author Milan Rukavina <rukavinamilan@gmail.com>
 * @version 0.1
 * @package whq
 * @subpackage module
 */
 
class WHQM_Router {
	/**
	 * Singleton instance
	 * @var WHQM_Router
	 */
	protected static $_instance = null;
    
    const STATUS_OK = 200;
    const STATUS_CREATED = 201;
    const STATUS_NOT_MODIFIED = 204;
    const STATUS_BAD_REQUEST = 400;
    const STATUS_UNAUTHORIZED = 401;
    const STATUS_FORBIDDEN = 403;
    const STATUS_NOT_FOUND = 404;
    const STATUS_CONFLICT = 409;
    const STATUS_INTERNAL_SERVER_ERROR = 500;
    
	private $method;
	private $inputData = '';
    private $queueName = '';
    private $controllerName = null;
    private $requestParts = array();
    private $headers = array();

/**
 * returns singleton instance of WHQM_Router
 * @return WHQM_Router
 */
	public static function getInstance()
	{
		if(self::$_instance === null)
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	private function __construct(){
		//process uri
		$uri = $_SERVER["REQUEST_URI"];
		// Kill the query string
		$uri = explode("?",$uri);
		$uri = $uri[0];
        $uri = trim($uri, '/');
		//get uri path elements
        $parts = explode('/', $uri);
        $startIndex = array_search('whqm', $parts);

        if($startIndex === FALSE){
            return;
        }
        
        for($i = 0; $i <= $startIndex; $i++){
            array_shift($parts);
        }

        $this->requestParts = $parts;

        $this->queueName = isset ($parts[0])?$parts[0]:null;
        if($this->queueName){
            $queueParts = explode('.', $this->queueName);
            $controllerParts = array();
            foreach ($queueParts as $i => $part) {
                $controllerParts[] = ucfirst(strtolower($part));
            }
            $this->controllerName = implode('_', $controllerParts);
        }
		
		//get method
		$this->method=$_SERVER["REQUEST_METHOD"];
		//input data for post (create) and put (update)
		if ($this->method=="POST" || $this->method=="PUT") {
			$this->inputData = file_get_contents("php://input");
		}

        $this->headers = getallheaders();
	}

	public static function headerStatus($code){
		$statuses = array(
			200	=> "OK",
			201 => "CREATED",
			304	=> "NOT MODIFIED",
			400	=> "BAD REQUEST",
			401	=> "UNAUTHORIZED",
			403	=> "FORBIDDEN",
			404	=> "NOT FOUND",
			409 => "CONFLICT",
			500	=> "INTERNAL SERVER ERROR"
		);

		header("HTTP/1.0 " . $code . " " . $statuses[$code],true,$code);
	}

	private function getControllerClassName($controllerName){
        return 'WHQM_Controller_' . $controllerName;
	}
	
	public function dispatch(){
        if(!$this->controllerName){
            self::headerStatus(self::STATUS_BAD_REQUEST);
            return false;
        }

        $controllerClassName = $this->getControllerClassName($this->controllerName);

        if(class_exists($controllerClassName)){
            $controller = new $controllerClassName($this->queueName,$this->requestParts);
            if(method_exists($controller,$this->method)){
                echo $here;
                return $controller->{$this->method}($this->inputData,$this->headers);
            }
            else{
                self::headerStatus(self::STATUS_BAD_REQUEST);
                return false;
            }
        }
        else{
            self::headerStatus(self::STATUS_NOT_FOUND);
            return false;
        }
	}
}
	
?>